<template>
  <div class='channel-edit'>
    <van-cell title="我的频道" center :border="false" class="channel-title">
      <!-- 使用 right-icon 插槽来自定义右侧图标 -->
      <template #right-icon>
        <van-button
        plain type="info"
        round size="mini"
        @click="isEdit=!isEdit"
        >{{isEdit ? '完成' : '编辑'}}</van-button>
      </template>
    </van-cell>

    <van-grid :gutter="10">
      <van-grid-item
      class="grid-item "
      :class="{active : index === active}"
      :icon="(isEdit && index !==0) ? 'clear' : ''"
      v-for="(channel,index) in userChannels"
      :key="index"
      :text="channel.name"
      @click="onUserChannelClick(channel,index)"
      />
    </van-grid>

    <van-cell title="频道推荐" center :border="false" class="channel-title">
    </van-cell>

    <van-grid :gutter="10">
      <van-grid-item class="grid-item" v-for="(channel,index) in recommendChannels" :key="index" :text="channel.name"
      @click="onAdd(channel)" />
    </van-grid>
   </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { setItem } from '@/utils/storage'
export default {
  name: 'channel-edit',
  components: {},
  props: {
    // 'user-channels'
    userChannels: {
      type: Array,
      required: true
    },
    allChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      isEdit: false
    }
  },
  created () {},
  computed: {
    ...mapGetters(['token']),

    // 推荐频道 全部频道-我的频道=剩下的频道
    recommendChannels () {
      return this.allChannels.filter(channel => {
        // 频道channel 是否属于用户频道
        return !this.userChannels.find(userChannel => {
          // 找到满足条件的元素
          return userChannel.id === channel.id
        })
      })
    }
  },
  methods: {
    onAdd (channel) {
      // this.userChannels.push(channel) // 直接改了父组件传过来的值了

      // 数据持久化
      if (this.token) {
        // 登录了，数据存到数据库
      } else {
        // 未登录，数据存储到本地
        setItem('user-channels', this.userChannels)
      }
    },
    onUserChannelClick (channel, index) {
      if (this.isEdit && index !== 0) {
        // 如果是编辑状态 则 删除频道
        this.deleteChannel(channel, index)
      } else {
        // 非编辑状态 则 进入频道
        this.switcChannel(index)
      }
    },
    deleteChannel (channel, index) {
      console.log('删除频道')
      // 如果删除的是当前激活频道之前的频道
      if (index <= this.active) {
        // 更新激活频道的索引
        this.$emit('update-active', this.active - 1)
      }
      // this.userChannels.splice(index, 1) // 直接改了父组件传过来的值了

      // 数据持久化
      if (this.token) {
        // 登录了，数据存到数据库
      } else {
        // 未登录，数据存储到本地
        setItem('user-channels', this.userChannels)
      }
    },
    switcChannel (index) {
      console.log(' 切换频道')
      // 切换频道
      this.$emit('update-active', index)
      // 关闭弹出层
      this.$emit('close')
    }
  }
}
</script>
<style lang="scss" scoped>
.channel-edit{
  padding-top: 54px;
}
.channel-title{
  font-size: 16px;
  color: #333333;
}
.grid-item{
  width: 80px;
  height: 43px;
  ::v-deep .van-grid-item__content{
    background-color: #f4f5f6;
    .van-grid-item__text{
      font-size: 14px;
      color: #222;
      margin-top:0;
    }
  }

  ::v-deep .van-grid-item__icon{
    position: absolute;
    right:-10px;
    top:-5px;
    font-size: 20px;
    color: #ccc;
  }
}
.active{
  ::v-deep .van-grid-item__text{
      color: red !important;
    }
  }
</style>
